package com.example.zhanglinan.vrdemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhanglinan.vrdemo.R;


/**
 * @author Schutzengel
 *         o(＞﹏＜)o
 *         create on 2017/1/27 14:39
 *         $desc$
 */
public abstract class BaseFragment extends Fragment {

    private SwipeRefreshLayout mSwpBase;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, container, false);
        RecyclerView rcBase = (RecyclerView) view.findViewById(R.id.rv_base);
        mSwpBase = (SwipeRefreshLayout) view.findViewById(R.id.srl_base);
        rcBase.setLayoutManager(getManager());
        rcBase.setAdapter(getAdapter());
        rcBase.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                onAdd(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mSwpBase.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light,
                android.R.color.holo_blue_bright, android.R.color.holo_orange_light);
        mSwpBase.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onFresh();
            }
        });
        return view;
    }

    public abstract RecyclerView.LayoutManager getManager();

    public abstract RecyclerView.Adapter getAdapter();

    public abstract void onFresh();

    public abstract void onAdd(RecyclerView recyclerView, int newState);

    public void onComplete() {
        mSwpBase.setRefreshing(false);
    }
}
